<?php
/**
 * Creater: 李青林
 * Date Time : 2018/9/20 16:11
 */

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\User;
use App\Http\Models\UserIntegralLog;

class IntegralLogController extends Controller
{
    use ModelForm;
    protected $headerStr = '积分';

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->headerStr . '管理');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(UserIntegralLog::class, function (Grid $grid) {
            $grid->model()->select('users_integral_log.id', 'users.name', 'users.avatar',
                'users_integral_log.content', 'users_integral_log.status', 'users_integral_log.number', 'users_integral_log.user_id','users_integral_log.balance','users_integral_log.created_at'
            )->leftjoin('users', 'users.id', '=', 'users_integral_log.user_id')->orderBy('id', 'desc');
            $grid->id('编号')->sortable();
            $grid->user_id('用户ID(昵称)')->display(function () {
                return $this->user_id . '(' . $this->name . ')';
            })->sortable();
            $grid->avatar('头像')->display(function ($avatar) {
                return user_image_url($avatar);
            })->image(40, 40);
            $grid->number('数量')->display(function ($number) {
                $s = ($this->status > 0) ? '+ ' : '- ';
                return $s . $number;
            });
            $grid->balance('余额');
            $grid->content('描述');
            $grid->created_at('时间');
            $grid->status('类型')->display(function ($status) {
                return ($status == 0) ? '<span class=" label label-success">被扣</span>' : '<span class=" label label-primary">增加</span>';
            });
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->disableActions();
            $grid->paginate(env('Admin_Limit'));
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();//禁用查询过滤器
                $filter->equal('user_id', '用户ID');
                $filter->like('name', '用户昵称');
            });
        });
    }


}